package sort;
import logger.*;
import java.io.*;
import java.util.Arrays;


/**
 *
 * @author BDSHADOW
 */
public class Sort {
    public static void main(String[] args) throws IOException {
        long[] time = new long[10];
        int next = 0;
        for (int k = 0; k < 10; k++) {
           long startTime = System.nanoTime();
            Logger logger = new Logger();
            logger.addFileAppender("logger.txt", true, 200);
            logger.setPattern("%t - %l - %m");
            logger.setLevel(LogLevel.INFO);

            logger.trace("quaquauqua");
            logger.fatal("uzhs prosto");
            logger.error("bugbugbug");
            logger.stop();

            
            BigReadWrite data = new BigReadWrite();
            
            int[] a = data.readArray(args[0]);
            //mergesort(a, 0, a.length-1);
            Arrays.sort(a);
            data.writeArray(a, args[1]);
            long endTime = System.nanoTime();
            time[next++] = endTime-startTime;
        }
        long average = 0;
        for (int i = 0; i < 10; i++) {
           average += time[i];
        }
        System.out.println(average/10);
    }
    
     private static void mergesort (int[] a, int low, int high) { 
          if (low < high) {
              int m = (low+high)/2; 
              mergesort (a, low, m); 
              mergesort (a, m+1, high); 
              merge (a, low, m, high);
          }
     } 

     private static void merge (int[] a, int low, int m, int high) { 
          if (m+1>high) return; 
          int[] b = new int[a.length]; 
          for (int i=low; i!=m+1; i++) { 
               b[i] = a[i]; 
          } 

          for (int i=m+1; i!=high+1; i++) { 
               b[i] = a[high+m+1-i]; 
          }

          int k=low; int j=high;
          for (int i=low; i!=high+1; i++) { 
               if (b[k] <= b[j]) {
                   a[i] = b[k++];
               } else {
                    a[i] = b[j--];
               }
          } 
     }
}
